package com.lincoln.redis.server.utils;

import com.alibaba.fastjson.JSONObject;

/**
 * 字符串转各种类型
 *
 * @author lincoln
 */
public class StringCastUtil {
    public static <T> Object cast(Class<T> type, String value) {
        if (type == String.class) {
            return value;
        }
        if (type == Long.class) {
            return Long.valueOf(value);
        }
        if (type == Integer.class) {
            return Integer.valueOf(value);
        }
        if (type == Double.class) {
            return Double.valueOf(value);
        }
        if (type == Short.class) {
            return Short.valueOf(value);
        }
        if (type == Float.class) {
            return Float.valueOf(value);
        }
        if (type == Character.class) {
            return Character.valueOf(value.charAt(0));
        }
        if (type == Byte.class) {
            return Integer.valueOf(value);
        }
        if (type == Boolean.class) {
            return Boolean.valueOf(value);
        }

        if (type == String[].class) {
            return JSONObject.parseArray(value).toArray(new String[0]);
        }
        if (type == Long[].class) {
            return JSONObject.parseArray(value).toArray(new Long[0]);
        }
        if (type == Integer[].class) {
            return JSONObject.parseArray(value).toArray(new Integer[0]);
        }
        if (type == Double[].class) {
            return JSONObject.parseArray(value).toArray(new Double[0]);
        }
        if (type == Short[].class) {
            return JSONObject.parseArray(value).toArray(new Short[0]);
        }
        if (type == Float[].class) {
            return JSONObject.parseArray(value).toArray(new Float[0]);
        }
        if (type == Character[].class) {
            return JSONObject.parseArray(value).toArray(new Character[0]);
        }
        if (type == Byte[].class) {
            return JSONObject.parseArray(value).toArray(new Byte[0]);
        }
        if (type == Boolean[].class) {
            return JSONObject.parseArray(value).toArray(new Boolean[0]);
        }
        return null;

    }
}
